<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="application/javascript" src="angularjs/angular.min.js"></script>
    <script>
        var app = angular.module('myApp',[]);
        app.controller('myController',function ($scope,$http) {

            $scope.findAll = function () {
                $http.get('findAll.do').success(function (response) {
                    $scope.list=response;
                })
            }
            $scope.toAdd = function () {
                $scope.player={};
                $scope.initschool();
                $scope.initteam();
                document.getElementById("add").style.display="block";
            }
            $scope.savePlayer = function () {
                $http.post('savePlayer.do',$scope.player).success(function (response) {
                    if(response.flag){
                        $scope.findAll();
                        document.getElementById("add").style.display="none";
                    }else{
                        alert(response.msg);
                    }
                })
            }
            $scope.initschool = function () {
                $http.get("getSchool.do").success(function (response) {
                    $scope.Schoollist=response;
                });
            }
            $scope.initteam = function () {
                $http.get("getTeam.do").success(function (response) {
                    $scope.Teamlist=response;
                });
            }
            $scope.ids =[];
            $scope.updateSelection = function ($event,id) {
                if($event.target.checked){
                    $scope.ids.push(id);
                }else{
                    //这个是用户取消选择了，我们需要把用户取消的这个id在数组中的位置找出来，下标
                    var index = $scope.ids.indexOf(id);
                    $scope.ids.splice(index,1);
                }
            };
            $scope.deleteStu = function () {
                $http.post("deletePlayer.do",$scope.ids).success(function (response) {
                    if(response.flag){
                        $scope.findAll();
                    }else{
                        alert(response.msg);
                    }
                })
            }

        });
    </script>
</head>

<body ng-app="myApp" ng-controller="myController" ng-init="findAll()">
<table border="1" rules="all">
    <tr align="center" bgcolor="#00ffff">
        <th><input type="checkbox"/></th>
        <th>id</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>生日</th>
        <th>学校名称</th>
        <th>队伍名称</th>
        <th>队伍地址</th>
        <th><input type="button" ng-click="toAdd()" value="新增"/>
        </th>
    </tr>
    <tr align="center" ng-repeat="s in list">
        <td><input type="checkbox" ng-click="updateSelection($event,s.id)"/></td>
        <td>{{s.id}}</td>
        <td>{{s.pname}}</td>
        <td>{{s.page}}</td>
        <td>{{s.birthday}}</td>
        <td>{{s.school.sname}}</td>
        <td>{{s.team.tname}}</td>
        <td>{{s.team.tarea}}</td>
    </tr>
    <tr>
        <td colspan="100">
            <input type="button" value="批量删除" ng-click="deleteStu()"/>
        </td>
    </tr>
</table>
<div id="add" style="display: none">
    <table border="1" rules="all">
        <tr align="center">
            <td>姓名</td>
            <td>
                <input type="text" ng-model="player.pname"/>
            </td>
        </tr>
        <tr align="center">
            <td>年龄</td>
            <td>
                <input type="text" ng-model="player.page"/>
            </td>
        </tr>
        <tr align="center">
            <td>生日</td>
            <td>
                <input type="text" ng-model="player.birthday"/>
            </td>
        </tr>
        <tr align="center">
            <td colspan="100">
                请选择:
                <select ng-model="player.sid">
                    <option value="-1">--请选择学校--</option>
                    <option ng-repeat="p in Schoollist" ng-value="p.sid">{{p.sname}}</option>
                </select>省-
            </td>
        </tr>
        <tr align="center">
            <td colspan="100">
                请选择:
                <select ng-model="player.tid">
                    <option value="-1">--请选择球队--</option>
                    <option ng-repeat="s in Teamlist" ng-value="s.tid">{{s.tname}}</option>
                </select>省-
            </td>
        </tr>
        <tr align="center">
            <td colspan="100">
                <input type="button" value="保存" ng-click="savePlayer()"/>
            </td>
        </tr>
    </table>
</div>
</body>
</html>